using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ArabicNLP_Project
{
    public abstract class Table
    {
        protected string globalPath = @"..\..\Data\";
        protected Dictionary<string,object> hashTable;
        protected Dictionary<string, List<string>> FilterCache;
        public Table()
        {
            hashTable = new Dictionary<string, object>();
            FilterCache = new Dictionary<string, List<string>>();
        }
        public void add(string entry)
        {
            hashTable.Add(entry, null);
        }
        public void add(string entry,object data)
        {
            hashTable.Add(entry, data);
        }

        public bool find(string entry)
        {
            return (hashTable.ContainsKey(entry));            
        }
        public object getData(string entry)
        {
            if (find(entry))
            {
                return hashTable[entry];
            }
            else
            {
                return null;
            }
        }


        public void AddFile(string path)
        {
            string[] lines = File.ReadAllLines(globalPath+path);
            string categoryName =null;
            foreach (string line in lines)
            {
                string _line = line.Trim();
                if (_line.StartsWith("--"))
                {
                    // comment
                    categoryName = _line.TrimStart('-');
                }
                else
                {
                    string[] words = _line.Split(new char[] { ' ' } , StringSplitOptions.RemoveEmptyEntries);
                    foreach (string word in words)
                    {
                        if (!hashTable.ContainsKey(word))
                        {
                            List<string> data = new List<string>();
                            data.Add(categoryName);
                            hashTable.Add(word, data);
                        }
                        else
                        {
                            ((List<string>)hashTable[word]).Add(categoryName);
                        }
                    }
                }
            }
        }

        public List<string> filter(string value)
        {
            if (FilterCache.ContainsKey(value))
            {
                return FilterCache[value];
            }
            List<string> results = new List<string>();
            string[] keys = new string[hashTable.Keys.Count];
            hashTable.Keys.CopyTo(keys, 0);
            for (int i = 0; i < keys.Length; i++)
            {
                foreach (string keyData in (List<string>)hashTable[keys[i]])
                    if (keyData  == value)
                        results.Add(keys[i]);  
            }
            FilterCache[value] = results;
            return FilterCache[value];
        }
        public List<string> filter(string value, string s)
        {
            List<string> results = new List<string>();
            results.Add(s);
            results.AddRange(filter(value));
                        
            return results;
        }
        public string[] list()
        {
            string[] keys = new string[hashTable.Keys.Count];
            hashTable.Keys.CopyTo(keys, 0);
            return keys;
        }
        public void clear()
        {
            hashTable.Clear();
            FilterCache.Clear();
        }
    }
}
